package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.itemshop;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.StorePanel;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.BagClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.CharacterClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventListener;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.managers.StoreManager;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.Timer;

class InventoryPanel extends JPanel implements MouseListener, KeyListener, GraphicEventListener, ActionListener{
	
	private GraphicEventManager gem = GraphicEventManager.getInstance();
	final int TILE_SIZE = 88;
	final int ROWS = 2;
	final int COLUMNS = 4;
	private int x;
	private int y;
	final private int IMAGE_ITEM_SIZE = 64;
	private StoreManager storeManager;
	private float alpha = MAX_ALPHA;
	static private float ALPHA_AMOUNT = 0.02f;
	static private float MIN_ALPHA = 0.6f;
	static private float MAX_ALPHA = 1.0f;
	private ImageCache imgCache;
	private Image bagImg;
	private HashMap<String, String> settings;
	private Timer timer = new Timer(40, this);
	
	
	InventoryPanel(StoreManager storeManager, HashMap<String,String> settings, ImageCache imgCache) {
		this.setOpaque(false);
		this.imgCache = imgCache;
		this.settings = settings;
		this.setPreferredSize(new Dimension(TILE_SIZE*COLUMNS, TILE_SIZE*ROWS));
		this.storeManager = storeManager;
		this.addMouseListener(this);
		this.addKeyListener(this);
		gem.addListener(this, CharacterClickEvent.class);
		bagImg = imgCache.getImage(this.settings.get(StorePanel.BAG_IMAGE));

	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;

		for(int y = 0; y<ROWS; y++)
			for(int x = 0; x<COLUMNS; x++)
				if(y != this.y || x != this.x)
				g2d.drawImage(bagImg, x*TILE_SIZE, y*TILE_SIZE, null);
			
		if(x != -1 && y != -1){
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    alpha));
			g2d.drawImage(bagImg, x*TILE_SIZE,y*TILE_SIZE, null);
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, MAX_ALPHA));
		}
		int offset = (TILE_SIZE - IMAGE_ITEM_SIZE)/2;
		
		if(storeManager.getCurrentSelectedChar() != null)
			  for(int y = 0; y<ROWS; y++)
					for(int x = 0; x<COLUMNS; x++){
						Image itemImg = null;
						String itemName = storeManager.getCurrentSelectedChar().getInventory().getItem(x+(COLUMNS*y));
						if( itemName != null)
							itemImg = imgCache.getImage(settings.get(StorePanel.ITEM_IMAGE_PATH)+itemName+settings.get(StorePanel.ITEM_IMAGE_EXTENSION));

						g2d.drawImage(itemImg, x*TILE_SIZE+offset, y*TILE_SIZE+offset, null);
					}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e){};

	@Override
	public void mouseClicked(MouseEvent e) {}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {
	
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		x = e.getX();
		y = e.getY();
		alpha = MAX_ALPHA;
		if(x>0 && x<COLUMNS*TILE_SIZE && y>0 && y<ROWS*TILE_SIZE){
			x = x/TILE_SIZE;
			y = y/TILE_SIZE;
			int clickedBag = x +COLUMNS*y;
			storeManager.setCurrentSelectedBag(clickedBag);
			gem.fireEvent(new BagClickEvent(clickedBag));
			timer.start();
		}
		repaint();
	}

	@Override
	public void notify(GraphicEvent event) {
		
		if(event.getClass() == CharacterClickEvent.class){
			timer.stop();
			x = y = -1;
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		 alpha += ALPHA_AMOUNT;
			if(alpha < MIN_ALPHA){
				alpha = MIN_ALPHA;
				ALPHA_AMOUNT *= -1;
			} else if(alpha > MAX_ALPHA){
				alpha = MAX_ALPHA;
				ALPHA_AMOUNT *= -1;
			}
		repaint();
	}



}
